from base import mysqlConnect

# kkl_kapi库中维护的接口一览
def showList():
    db, cursor = mysqlConnect.connect("sit", "kkl_kapi")
    selectsql = "SELECT methodName,sign,methodDesc FROM t_method WHERE `status`=1 AND deleted='N' AND sign like 'la.kaike%' GROUP BY sign;"
    cursor.execute(selectsql)
    interfaces = cursor.fetchall()
    db.close()
    return interfaces

# 校验interfacelist中的接口是否在kkl_zhiliang中维护，返回未维护的接口列表
def showNewList(interfacelist):
    newList = []
    db, cursor = mysqlConnect.connect("kkl_zhiliang", "kkl_zhiliang")
    for result in interfacelist:
        interfaceName = result[0]
        checksql = "SELECT ISNULL((SELECT 1 from t_interfacetest_check WHERE interfaceName='%s' AND deleted='N'))"%(interfaceName)
        cursor.execute(checksql)
        (checkResult,) = cursor.fetchone()  # 1不存在 0存在
        if checkResult == 1:
            newList.append(result)
    db.close()
    return newList

def insertNewList(newList):
    db, cursor = mysqlConnect.connect("kkl_zhiliang", "kkl_zhiliang")
    for interfaceData in newList:
        interfaceName = interfaceData[0]
        interfaceUrl = interfaceData[1]
        interfaceDesc = interfaceData[2]
        insertsql = "INSERT INTO `t_interfacetest_check` (`interfaceName`, `interfaceUrl`, `interfaceDesc`, `status`, `owner`, `remark`, `deleted`) VALUES ('{}', '{}', '{}', '0', 'null', 'null', 'N');".format(interfaceName, interfaceUrl, interfaceDesc)
        print(insertsql)
        try:
            cursor.execute(insertsql)
            db.commit()
        except:
            db.rollback()
    db.close()

def compeleteTask(updateList):
    db, cursor = mysqlConnect.connect("kkl_zhiliang", "kkl_zhiliang")
    for interfacename in updateList:
        updatesql = "UPDATE t_interfacetest_check SET `status`=1 WHERE interfaceUrl LIKE '%{}';".format(interfacename)
        try:
            print(updatesql)
            cursor.execute(updatesql)
            db.commit()
        except:
            db.rollback()


